package com.hama.iotrust.common.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.ImageIcon;

import com.hama.iotrust.common.ServiceException;

/**
 *图片工具类，主要用于剪裁图片 
 * @author liyang
 */
public class ImgUtil {

	/**
	 * 图片裁剪,返回裁剪之后的文件流
	 * @param in 图片流
	 * @param dest 裁剪之后的存储路径
	 * @param suffixName 图片后缀名
	 * @param x  图片裁剪起点x坐标
	 * @param y  图片裁剪起点y坐标
	 * @param w  图片裁剪宽度
	 * @param h  图片裁剪高度
	 */
	public static InputStream cutImage(InputStream in, String dest, String suffixName, int x, int y, int w, int h){
		try {
			InputStream is = null;
			
			Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName(suffixName);
			ImageReader reader = iterator.next();
			
			
			//图片流转换成图片并裁剪
			ImageInputStream iis = ImageIO.createImageInputStream(in);
			reader.setInput(iis, true);
			ImageReadParam param = reader.getDefaultReadParam();
			Rectangle rect = new Rectangle(x, y, w, h);
			param.setSourceRegion(rect);
			BufferedImage bi = reader.read(0, param);
			iis.close();
			
			//将裁剪之后的图片转换成输入流，返回给程序
			ByteArrayOutputStream bs = new ByteArrayOutputStream();
			//ImageOutputStream imgOut = ImageIO.createImageOutputStream(bs);
			//ImageIO.write(bi, suffixName, imgOut);
			ImageIO.write(bi, suffixName, bs);
			is = new ByteArrayInputStream(bs.toByteArray());
			bs.close();
			
			return is;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new ServiceException(e.getMessage(), e);
		}finally {
			
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				throw new ServiceException(e.getMessage(), e);
			}
			
		}
			
	}
	
	/**
	 * 缩放图片，返回处理之后的文件流
	 * @param in 图片流
	 * @param suffixName 图片后缀名
	 * @param minWidth 要缩放到的宽度
	 * @param minHeight 要缩放到的高度
	 */
	public static InputStream scaleImg(InputStream in, String suffixName, int minWidth, int minHeight){
		try {
			InputStream is = null;
	
			//获取原图
			ImageInputStream iis = ImageIO.createImageInputStream(in);
			BufferedImage temp = ImageIO.read(iis);
			Image oriImg = (Image)temp;
//			byte[] b = readStream(in);
//			ImageIcon ii = new ImageIcon(b);
//			Image oriImg = ii.getImage();
			
			
//			if(oriImg.getWidth(null) > minWidth && oriImg.getHeight(null) > minHeight){
				//按照给定宽高缩放
				Image resizeImg = oriImg.getScaledInstance(minWidth, minHeight, Image.SCALE_SMOOTH);
				
				//将缩放之后的图片转换成输入流
				BufferedImage bi = new BufferedImage(resizeImg.getWidth(null), resizeImg.getHeight(null), 
						BufferedImage.TYPE_INT_RGB);			
				Graphics g = bi.getGraphics();
				g.setColor(Color.white);
				g.fillRect(0, 0, resizeImg.getWidth(null), resizeImg.getHeight(null));
				g.drawImage(resizeImg, 0, 0, null);
				g.dispose();
					
				ByteArrayOutputStream bs = new ByteArrayOutputStream();
				ImageIO.write(bi, suffixName, bs);
				is = new ByteArrayInputStream(bs.toByteArray());
				bs.close();
				
				//测试用，输出图片到本地
//				FileOutputStream out = new FileOutputStream(new File("D:\\flowers_1.jpg"));
//				ImageIO.write(bi, suffixName, out);
//				out.close();	
//			}
//			else{
//				is = in;
//			}

			return is;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new ServiceException(e.getMessage(), e);
		}finally {
			
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				throw new ServiceException(e.getMessage(), e);
			}
			
		}
	}
	
	
	/**
	 * 根据手机客户端需要的图片宽高比重新生成带留白的图片
	 * @param in 图片流
	 * @param rate 目标宽高比
	 * @param suffixName 图片后缀名
	 */
	public static InputStream resetImgSize(InputStream in, float rate, String suffixName){
		try {
			InputStream is = null;
			
			//获取原图
			ImageInputStream iis = ImageIO.createImageInputStream(in);
			BufferedImage temp = ImageIO.read(iis);
			Image oriImg = (Image)temp;
			
			//原图宽高比
			int oriWidth = oriImg.getWidth(null);
			int oriHeight = oriImg.getHeight(null);
			
			float oriRate = (float)oriWidth/(float)oriHeight;
			 
			//按照给定宽高比率生成一张新图，留白
			BufferedImage bi;
			if(oriRate >= rate){
				 bi = new BufferedImage(oriWidth, (int)(oriWidth/rate), BufferedImage.TYPE_INT_RGB);
			}
			else{
				 bi = new BufferedImage((int)(oriHeight*rate), oriHeight, BufferedImage.TYPE_INT_RGB);
			}
			
			//新图宽高
			int resWidth = bi.getWidth();
			int resHeight = bi.getHeight();
					
			Graphics g = bi.getGraphics();
			g.setColor(Color.white);
			g.fillRect(0, 0, resWidth, resHeight);
			g.drawImage(oriImg, (int)(resWidth/2-oriWidth/2), (int)(resHeight/2-oriHeight/2), null);
			g.dispose();
				
			ByteArrayOutputStream bs = new ByteArrayOutputStream();
			ImageIO.write(bi, suffixName, bs);
			is = new ByteArrayInputStream(bs.toByteArray());
			bs.close();
		
			//测试用，输出图片到本地
//			FileOutputStream out = new FileOutputStream(new File("D:\\flowers_1.jpg"));
//			ImageIO.write(bi, suffixName, out);
//			out.close();
			
			return is;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new ServiceException(e.getMessage(), e);
		}finally {
			
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				throw new ServiceException(e.getMessage(), e);
			}
			
		}

	}
	
	/**
	 * 改变图片格式 
	 * @param in 图片流
	 * @param targrtSuffix 修改后的后缀名
	 */
	public static InputStream changeSuffix(InputStream in , String targrtSuffix){
		try {
			InputStream is = null;

			ImageInputStream iis = ImageIO.createImageInputStream(in);
			BufferedImage temp = ImageIO.read(iis);
			
			BufferedImage bi = new BufferedImage(temp.getWidth(), temp.getHeight(), BufferedImage.TYPE_INT_RGB);
			Graphics g = bi.getGraphics();
			g.setColor(Color.white);
			g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
			g.drawImage(temp, 0, 0, null);
			g.dispose();
			
			ByteArrayOutputStream bs = new ByteArrayOutputStream();
			ImageIO.write(bi, targrtSuffix, bs);
			is = new ByteArrayInputStream(bs.toByteArray());
			bs.close();
			
			return is;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new ServiceException(e.getMessage(), e);
		}finally {
			
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				throw new ServiceException(e.getMessage(), e);
			}
			
		}
	}
	
//	public static void main(String[] args) {
//		try {
//			File oriFile = new File("D:\\仲村由里.jpg");
//			
//			BufferedImage bi = ImageIO.read(oriFile);
//			
//			ByteArrayOutputStream bs = new ByteArrayOutputStream();
//			ImageIO.write(bi, "jpg", bs);
//			InputStream is = new ByteArrayInputStream(bs.toByteArray());
//			bs.close();
//			
//			resetImgSize(is, (float)2/(float)3, "jpg");
//			
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	
}
